<?php
namespace jy_newganfee;
defined('BY_JYA') or exit('error');
/*
	任务解锁
*/
class upfee extends \plugin{
	/*
		下单成功
	*/
	function addorder($order){
		foreach($order['goods'] as $gv){
			$nowgoods=$this->get('goods',array('goodsid'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$nowgoods['id'] || $nowgoods['ignore']){
				continue;
			}
			$nowgoods['total']=$gv['total'];
			$nowgoods['price']=$gv['price'];
			$nowgoods['total_price']=$gv['total_price'];
			$this->model('model')->set_neworder($order,$nowgoods);
			$this->canyu($order,$nowgoods);
			$this->model('jicha')->create($order,$nowgoods);
		}
		return false;
	}
	/*
		downs
	*/
	public function canyu($order,$goods=array()){
		$fromuser=pdo_getcolumn('jy_newganfee_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>$order['id'],
			'order_price'=>$order['price'],
			'goodscommissions'=>json_decode($goods['commissions'],true),
			'commission_money'=>0,
			'team_money'=>0,
			'red_money'=>0,
			'fen_futype'=>$basic['fen_futype']?:1,
		);
		//直推、间推
		for($i=0;$i<2;$i++){
				if(!$fromuser){
					break;
				}
				$member=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
				$level=$this->get('level',array('id'=>$member['level_id']));
				if(!$i || !$this->isnojiantui){
					if($i==1 && $this->fenxiaoset['is_jiantuione'] && $level['level']>=$this->lastlevel['level']){
						$level=$this->lastlevel;
					}
					$this->create_commission($member,$level,$i);
				}
				$this->lastlevel=$level;
				$level['level']=$level['level']?:0;
				$fromuser=$member['fromuser'];
		}
		//自购升级
		$this->model('member')->update_agent_level($order['mid'],true,$goods);
		//上级升级
		if($fromuser){
			$this->model('member')->update_agent_level($fromuser);
			//上上级升级
			$shfromuser=pdo_getcolumn('jy_newganfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
			if($shfromuser){
				$this->model('member')->update_agent_level($shfromuser);
			}
		}
		return false;
	}
	
	/*
		更新状态;
	*/
	function order_complete($orderid){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('orderid'=>$orderid));
		return true;
	}
	/*
		队长分享 直推、间推;
	*/
	function create_commission($agent,$level=array(),$i=0,$type=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['commission_'.$i];
			}else{
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['commission_'.$i];
			}
			//看直推等级有没有设置间推
			if(!$i && $this->fenxiaoset['is_jiantuione'] && !$commissions['commission_1']){
				$this->isnojiantui=true;
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['commission_'.$i];
					//看直推等级有没有设置间推
					if(!$i && $this->fenxiaoset['is_jiantuione'] && !$gv['commissions']['commission_1']){
						$this->isnojiantui=true;
					}
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$fufee=$this->model('model')->up_fee($money,$agent['mid']);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'fufee'=>$fufee,
					'realmoney'=>$money-$fufee,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>0,
					'level'=>$i,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				//if($i==0){
					//拿贡献奖
					$gongfee=$this->create_shangjifee($new_commission,$agent['mid'],1,5);
					//拿感恩奖
					$ganfee=$this->create_xiajifee($new_commission,$agent['fromuser'],1,4);
				//}
				$new_commission['gongfee']=$gongfee;
				$new_commission['ganfee']=$ganfee;
				$new_commission['realmoney']=$money-$fufee-$gongfee-$ganfee;
				$new_commission['realmoney']=$new_commission['realmoney']<0?0:$new_commission['realmoney'];
				$this->insert('commission',$new_commission);
			}
			return true;
	}
	/*
		拿感恩奖,拿下级收入;
	*/
	function create_xiajifee($new_commission,$fromuser=0,$status=0,$type=1){
		$ganfee=0;
		for($i=0;$i<3;$i++){
			if(!$fromuser){
				break;
			}
			$agent=pdo_get('jy_newganfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$fromuser=$agent['fromuser'];
			if(!$agent['level_id']){
				$commissions=$this->plugin_setting('default_level')['commissions'];
				$commission_per=$commissions['xiaji_'.$i];
			}else{
				$level=pdo_get('jy_newganfee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['xiaji_'.$i];
			}
			
			$money=0;
			$order_price=$new_commission['money'];		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['commissions']['xiaji_'.$i];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=$goods_commission_per;
					}
					break;
				}
			}	
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=$commission_per;
				}
			}
			if($money>0){
				$fufee=$this->model('model')->up_fee($money,$agent['mid']);
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'orderid'=>$new_commission['orderid'],
					'money'=>$money,
					'fufee'=>$fufee,
					'realmoney'=>$money-$fufee,
					'createtime'=>TIMESTAMP,
					'status'=>1,
					'type'=>$type,
					'level'=>$i,
					'fen_futype'=>$new_commission['fen_futype'],
				);
				$this->insert('commission',$new_commission);
				$ganfee +=$money;
			}
		}
		return $ganfee;
	}
	/*
		拿贡献奖,拿上级收入;
	*/
	function create_shangjifee($new_commission,$fromuser=0,$status=0,$type=2){
		$gongfee=0;
		$order_price=$new_commission['money'];
		for($i=0;$i<3;$i++){
			if(!$fromuser){
				break;
			}
			//获取本人等级的贡献奖比例
			$agent=pdo_get('jy_newganfee_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			if(!$agent['level_id']){
					$commissions=$this->plugin_setting('default_level')['commissions'];
					$commission_per=$commissions['shangji_'.$i];
			}else{
					$level=pdo_get('jy_newganfee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
					$level_id=$level['id'];
					$commissions=json_decode($level['commissions'],true);
					$commission_per=$commissions['shangji_'.$i];
			}
			$money=0;
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
					if($agent['level_id']==$gv['level_id']){
						$goods_commission_per=$gv['commissions']['shangji_'.$i];
						$isgoodsper=1;
						if(substr($goods_commission_per,-1)=='%'){
								$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
						}else{
								$money=$goods_commission_per;
						}
						break;
					}
			}
			if(!$isgoodsper){
					if(substr($commission_per,-1)=='%'){
						$money=intval($order_price*substr($commission_per,0,-1))/100;
					}else{
						$money=$commission_per;
					}
			}
			$members=pdo_getall('jy_newganfee_member',array('level_id >'=>0,'fromuser'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
			$count=count($members)?:0;
			if(!$count || $money<=0){
				continue;
			}
			$fromuser=array_keys($members);
			$onemoney=$money/$count;
			if($onemoney>0){
				foreach($members as $mv){
						$fufee=$this->model('model')->up_fee($onemoney,$mv['mid']);
						$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mv['mid'],
							'orderid'=>$new_commission['orderid'],
							'money'=>$onemoney,
							'fufee'=>$fufee,
							'realmoney'=>$onemoney-$fufee,
							'createtime'=>TIMESTAMP,
							'status'=>1,
							'type'=>$type,
							'level'=>$i,
							'fen_futype'=>$new_commission['fen_futype'],
						);
						$this->insert('commission',$new_commission);
						$gongfee +=$onemoney;
				}
			}
		}
		return $gongfee;
	}
	/*
		更新进入新参与;
	*/
	function up_daycanyus($mid){
		$this->model('member')->up_tixianfee($mid,true);
		$dayid=pdo_getcolumn('jy_newganfee_member',array('mid'=>$mid),'dayid');
		$newdayid=date('Y-m-d',TIMESTAMP);
		if($dayid==$newdayid){
			pdo_update('jy_newganfee_member',array('daycanyus +='=>1),array('mid'=>$mid));
		}else{
			pdo_update('jy_newganfee_member',array('daycanyus'=>1,'dayid'=>$newdayid),array('mid'=>$mid));
		}
		return true;
	}
}